package com.showde.shoppingmall.db;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.kim.androidbase.db.DBCommandInteface;
import com.kim.androidbase.db.DBSession;
import com.kim.androidbase.db.IDBUtils.QueryCompleteCallback;
import com.kim.androidbase.db.SQLiteCriteria;
import com.showde.shoppingmall.db.constant.ProductDescConstant;
import com.showde.shoppingmall.domain.ProductDesc;

public class ProductDescDBCommand implements
		DBCommandInteface<ProductDesc, String> {

	private static final String TAG = "ProductDescDBCommand";

	DBSession dbSession;

	public ProductDescDBCommand(Context context) {
		dbSession = new DBSession(context, new DBHelper(context));
	}

	@Override
	public void add(ProductDesc productDesc) {
		dbSession.insert(ProductDescConstant.TABLENAME,
				getProductDescContentValues(productDesc),
				SQLiteDatabase.CONFLICT_REPLACE);
	}

	@Override
	public void add(List<ProductDesc> ts) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(String pk) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(SQLiteCriteria sqLiteCriteria) {
		// TODO Auto-generated method stub

	}

	@Override
	public void modify(SQLiteCriteria sqLiteCriteria) {
		// TODO Auto-generated method stub

	}

	@Override
	public void modify(String pk, SQLiteCriteria sqLiteCriteria) {
		// TODO Auto-generated method stub

	}

	@Override
	public ProductDesc query(final String pk) {
		final ProductDesc productDesc = new ProductDesc();
		dbSession.query(ProductDescConstant.TABLENAME,
				ProductDescConstant.dbColumns,
				ProductDescConstant.PRODUCTDESCID + "=?", new String[] { pk },
				null, null, null, new QueryCompleteCallback() {

					@Override
					public void onQueryError(String msg) {
						Log.d(TAG, String.format("query[%s]", pk));
					}

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor != null && cursor.getCount() > 0) {
							while (cursor.moveToNext()) {
								getProductFromCursor(productDesc, cursor);
							}
						}
					}
				});
		return productDesc;
	}

	@Override
	public List<ProductDesc> query(SQLiteCriteria sqLiteCriteria) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ProductDesc> queryAll() {
		// TODO Auto-generated method stub
		return null;
	}

	private void getProductFromCursor(ProductDesc productDesc, Cursor cursor) {
		productDesc.setProductDescId(dbSession.getString(cursor,
				ProductDescConstant.PRODUCTDESCID));
		productDesc.setProductDescName(dbSession.getString(cursor,
				ProductDescConstant.PRODUCTDESCNAME));
		productDesc.setProductDescTitle(dbSession.getString(cursor,
				ProductDescConstant.PRODUCTDESCTITLE));
		productDesc.setProductDescPrice(dbSession.getFloat(cursor,
				ProductDescConstant.PRODUCTDESCPRICE));
		productDesc.setProductDescMPrice(dbSession.getFloat(cursor,
				ProductDescConstant.PRODUCTDESCMPRICE));
		productDesc.setProductDescVIPPrice(dbSession.getFloat(cursor,
				ProductDescConstant.PRODUCTDESCVIPPRICE));
		productDesc.setProductDescNum(dbSession.getInt(cursor,
				ProductDescConstant.PRODUCTDESCNUM));
		productDesc.setProductDescSizes(dbSession.getString(cursor,
				ProductDescConstant.PRODUCTDESCSIZES));
		productDesc.setProductDescColors(dbSession.getString(cursor,
				ProductDescConstant.PRODUCTDESCCOLORS));
		productDesc.setProductDescLogo(dbSession.getString(cursor,
				ProductDescConstant.PRODUCTDESCLOGO));
		productDesc.setProductDescDesc(dbSession.getString(cursor,
				ProductDescConstant.PRODUCTDESCDESC));
		productDesc.setProductDescPostFee(dbSession.getFloat(cursor,
				ProductDescConstant.PRODUCTDESCPOSTFEE));
		productDesc.setProductDescIsOutOfStack(dbSession.getBoolean(cursor,
				ProductDescConstant.PRODUCTDESCISOUTOFSTACK));
		productDesc.setProductCaregoryId(dbSession.getString(cursor,
				ProductDescConstant.PRODUCTCATEGORYID));
	}

	private ContentValues getProductDescContentValues(ProductDesc productDesc) {
		ContentValues contentValues = new ContentValues();

		contentValues.put(ProductDescConstant.PRODUCTDESCID,
				productDesc.getProductDescId());
		contentValues.put(ProductDescConstant.PRODUCTDESCNAME,
				productDesc.getProductDescName());
		contentValues.put(ProductDescConstant.PRODUCTDESCTITLE,
				productDesc.getProductDescTitle());
		contentValues.put(ProductDescConstant.PRODUCTDESCPRICE,
				productDesc.getProductDescPrice());
		contentValues.put(ProductDescConstant.PRODUCTDESCMPRICE,
				productDesc.getProductDescMPrice());
		contentValues.put(ProductDescConstant.PRODUCTDESCVIPPRICE,
				productDesc.getProductDescVIPPrice());
		contentValues.put(ProductDescConstant.PRODUCTDESCNUM,
				productDesc.getProductDescNum());
		contentValues.put(ProductDescConstant.PRODUCTDESCSIZES,
				productDesc.getProductDescSizes());
		contentValues.put(ProductDescConstant.PRODUCTDESCCOLORS,
				productDesc.getProductDescColors());
		contentValues.put(ProductDescConstant.PRODUCTDESCLOGO,
				productDesc.getProductDescLogo());
		contentValues.put(ProductDescConstant.PRODUCTDESCDESC,
				productDesc.getProductDescDesc());
		contentValues.put(ProductDescConstant.PRODUCTDESCPOSTFEE,
				productDesc.getProductDescPostFee());
		contentValues.put(ProductDescConstant.PRODUCTDESCISOUTOFSTACK,
				productDesc.isProductDescIsOutOfStack());
		contentValues.put(ProductDescConstant.PRODUCTCATEGORYID,
				productDesc.getProductCaregoryId());

		return contentValues;
	}
}
